﻿using Core.Utility.Helper;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Core.Utility.Security
{
    /// <summary>
    /// Api安全数据
    /// </summary>
    public class ApiSecurityData
    {
        /// <summary>
        /// 
        /// </summary>
        public string? Token { get; private set; }
        
        /// <summary>
        /// 
        /// </summary>
        public string? AppId { get; private set; }
        
        /// <summary>
        /// 
        /// </summary>
        public string? EncodingAESKey { get; private set; }

        /// <summary>
        /// Api安全数据
        /// </summary>
        /// <param name="token"></param>
        /// <param name="appId"></param>
        /// <param name="encodingAESKey"></param>
        public ApiSecurityData(string? token,string? appId,string? encodingAESKey)
        {
            Token = token;
            AppId = appId;
            EncodingAESKey = encodingAESKey;
        }

        /// <summary>
        /// 加密
        /// </summary>
        /// <param name="input">明文的数据字符串</param>
        /// <returns></returns>
        public ReturnResultInfo<string> Encrypt(string? input)
        {
            return ApiSecurity.AESEncrypt(input,AppId,EncodingAESKey);
        }

        /// <summary>
        /// 解密
        /// </summary>
        /// <param name="input">秘文的数据字符串</param>
        /// <param name="isVerifyAppId">是否验证AppId</param>
        /// <returns></returns>
        public ReturnResultInfo<string> Decrypt(string? input,bool isVerifyAppId = true)
        {
            string? appId = "";
            var decryptRes = ApiSecurity.AESDecrypt(input, AppId, ref appId);
            if(decryptRes.Successed)
            {
                if(isVerifyAppId)
                {
                    if(AppId != appId)
                    {
                        decryptRes.SetFailure("解密获取的AppId与当前使用的AppId不一致，请查证！");
                    }
                }
            }
            return decryptRes;
        }

        #region 设置参数
        /// <summary>
        /// 设置Token
        /// </summary>
        /// <param name="value"></param>
        public void SetToken(string? value)
        {
            Token = value;
        }

        /// <summary>
        /// 设置AppId
        /// </summary>
        /// <param name="value"></param>
        public void SetAppId(string? value)
        {
            AppId = value;
        }

        /// <summary>
        /// 设置EncodingAESKey
        /// </summary>
        /// <param name="value"></param>
        public void SetEncodingAESKey(string? value)
        {
            EncodingAESKey = value;
        }
        #endregion 设置参数
    }
}
